﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Configuration;
using L2C.Models;

namespace L2C.Controllers
{
    public class HomeController : Controller
    {
        L2CEntities entities = new L2CEntities(ConfigurationManager.ConnectionStrings["L2CEntities"].ToString());

        public ActionResult Index(int? Id)
        {
            var DealItems = new ListDealModel();
            var availableItems = entities.Publishing_Item.Top("101").Where(x => x.EndDate == null || x.EndDate > DateTime.Today).OrderByDescending(x => x.StartDate);
            var topItem = Id.HasValue ? availableItems.Where(x => x.Category_Id == Id.Value) : availableItems;
            DealItems.TopItem = topItem.Select(x => new L2C.Models.NewestItemModel { ID = x.Item_Id, Title = x.ShortTitle, Description = x.Descriptions, SellPrice = x.SellPrice, Image = x.Image, SEOCaption = x.SEOCaption, DiscountPercent = x.DiscountPercent }).FirstOrDefault();
            //var listItem = Id.HasValue ? entities.Publishing_Item.Where(x => x.Category_Id == Id.Value).OrderBy(x => x.Item_Id).Skip(1).Take(20) : entities.Publishing_Item.OrderBy(x => x.Item_Id).Skip(1).Take(20);
            DealItems.ListItem = topItem.Skip(1).Select(x => new L2C.Models.SmallItemModel { ID = x.Item_Id, Title = x.ShortTitle, SellPrice = x.SellPrice, Image = x.SmallPicture, SEOCaption = x.SEOCaption, DiscountPercent = x.DiscountPercent, ViewerCounter = x.ViewerCounter }).ToList();
            return View(DealItems);            
        }

        public ActionResult About()
        {
            return View();
        }

        public PartialViewResult ListDeal()
        { 
            var data = entities.Publishing_Item.OrderBy(x=> x.Item_Id).Skip(1).Select(x => new L2C.Models.SmallItemModel { ID = x.Item_Id, Title = x.ShortTitle, SellPrice = x.SellPrice , Image = x.SmallPicture, SEOCaption = x.SEOCaption}).ToList();
            return PartialView(data);
        }

        public PartialViewResult SideMenu()
        {
            var data = entities.Category_Item.Select(x=> new L2C.Models.SideMenuModel{ ID=x.Category_Id, Name=x.Category_Name}).ToList();
            return PartialView(data);
        }
    }
}
